const time = require("../../utils/getTime");
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    time: "",
    timeText: "",
    num: 1, //票数
    id: "", //openid
    username: "",
    telPhone: "",
    idCard: "", //身份证号码
  },

  onChange(event) {
    this.setData({
      //步进器改变时更改data里的票数
      num: event.detail,
    });
  },
  submitTicket() {
    //提交订票方法
    if (
      this.data.username == "" ||
      this.data.idCard == "" ||
      this.data.telPhone == "" ||
      this.data.id == ""
    ) {
      Toast('请正确输入');
      
    }else {
      const orderId = this.data.id.substring(0,4) + time.getTimeAll()
      wx.cloud.callFunction({
        name: 'add',
        data: {
          data: {
          openid: this.data.id,
          username: this.data.username,
          telphone: this.data.telPhone,
          id: this.data.idCard,
          num: this.data.num,
          orderid: orderId,
          time: this.data.timeText,
          week: time.getWeek()
          }
        },
        success: res => {
          console.log(res);
          Notify({ type: 'success', message: '订票成功' });
          this.setData({
            telPhone: '',
            idCard: '',
            num: 1
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取时间
    const userInfo = wx.getStorageSync("user"); //从本地存储里面拿用户名以及openid
    this.setData({
      time: time.getTime(),
      timeText: time.getTimeText() + '上午9:30入馆',
      username: userInfo.username,
      id: userInfo.id,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
